#include "record.h"

int record(int lcdp[][800])
{
	int i, j, ret, index = 0, flag = 0;
	char filename[20];
	unsigned char recho[480][800 * 3];
	FILE *recordcho = fopen("./res/recordcho.bmp", "r");
	fseek(recordcho, 54, SEEK_SET);
	fread(recho, 3, 480 * 800, recordcho);
	for (i = 0; i < 480; i++)
		for (j = 0; j < 800; j++)
		{
			int y = 479 - i;
			lcdp[y][j] = 0x00 << 24 | recho[i][j * 3 + 2] << 16 | recho[i][j * 3 + 1] << 8 | recho[i][j * 3];
		}
	int lcdt = open("/dev/input/event0", O_RDWR);
	if (lcdt < 0)
	{
		perror("input error\n");
		return -1;
	}
	struct input_event touch;
	int x = -1, y = -1;
	char showpage[20];
	font *show = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
	fontSetSize(show, 30);
	bitmap *page = createBitmapWithInit(50, 30, 4, getColor(255, 255, 255, 255));
	while (1)
	{
		sprintf(filename, "./lib/%s %d.txt", username, index);
		FILE *reco = fopen(filename, "r");
		if (reco == NULL)
		{
			flag = 0;
			unsigned char recho[480][800 * 3];
			FILE *recordcho = fopen("./res/recordcho-NULL.bmp", "r");
			fseek(recordcho, 54, SEEK_SET);
			fread(recho, 3, 480 * 800, recordcho);
			for (i = 0; i < 480; i++)
				for (j = 0; j < 800; j++)
				{
					int y = 479 - i;
					lcdp[y][j] = 0x00 << 24 | recho[i][j * 3 + 2] << 16 | recho[i][j * 3 + 1] << 8 | recho[i][j * 3];
				}
			goto change;
		}
		else
		{
			flag = 1;
			if (index > picnum)
				picnum++;
			printf("reading...\n");
			for (i = 0; i < 480; i++)
				for (j = 0; j < 750; j++)
					if (fscanf(reco, "%d", &lcdp[i][j]) != 1)
					{
						perror("read fail\n");
						return 1;
					}
			printf("record done\n");
			fclose(reco);
		}
	change:
		sprintf(showpage, "%s%d%s", "第", index, "页");
		page = createBitmapWithInit(70, 30, 4, getColor(255, 255, 255, 255));
		fontPrint(show, page, 0, 0, showpage, getColor(0, 0, 0, 255), 400);
		show_font_to_lcd(lcdp, 740, 50, page);
		printf("flag=%d index:%d\n", flag, index);
		while (1)
		{
			int read_ret = read(lcdt, &touch, sizeof(struct input_event));
			if (read_ret < 0)
			{
				perror("read touch failed\n");
				return -1;
			}
			if (touch.type == EV_ABS)
			{
				if (touch.code == ABS_X)
					x = touch.value * 800 / 1024;
				else if (touch.code == ABS_Y)
				{
					y = touch.value * 480 / 600;
				}
			}
			printf("x=%d y=%d\n", x, y);
			if (touch.type == EV_KEY)
			{
				if (touch.code == BTN_TOUCH && touch.value == 1)
				{
					if (x > 780 && x < 800 && y > 260 && y < 310 && flag == 1)
					{
						printf("进入跳转\n");
						ret = index;
						close(lcdt);
						goto done;
					}
					if (x > 780 && x < 800 && y > 120 && y < 190)
					{
						if (index < 1)
							index = 1;
						index--;
						printf("index:%d picnum:%d\n", index, picnum);
						break;
					}
					if (x > 780 && x < 800 && y > 210 && y < 250)
					{
						if (index < 1)
							index = 0;
						index++;
						printf("index:%d picnum:%d\n", index, picnum);
						break;
					}
					if (x > 780 && x < 800 && y > 320 && y < 390 && flag == 1)
					{
						int del = remove(filename);
						if (del == 0)
						{
							picnum--;
							char errorbuf[] = "已删除此记录:(";
							font *ef = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
							fontSetSize(ef, 50);
							bitmap *er = createBitmapWithInit(250, 45, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, errorbuf, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 265, 380, er);
							sleep(1);
							continue;
						}
						else
						{
							char errorbuf[] = "记录删除失败:(";
							font *ef = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
							fontSetSize(ef, 50);
							bitmap *er = createBitmapWithInit(250, 45, 4, getColor(255, 255, 255, 255));
							fontPrint(ef, er, 0, 0, errorbuf, getColor(0, 0, 0, 255), 400);
							show_font_to_lcd(lcdp, 265, 380, er);
							sleep(1);
							ret = -1;
							close(lcdt);
							goto done;
						}
					}
					if (x > 780 && x < 800 && y > 400 && y < 480)
					{
						printf("exit record\n");
						ret = -1;
						close(lcdt);
						goto done;
					}
				}
			}
		}
	}
done:
	fclose(recordcho);
	return ret;
}